<template>
	<view class="container">
		<view class="form-box">
			<view class="form">
				<list-cell :hover="false">
					<view class="form-input">
						<view class="label">收货人</view>
						<input class="input" placeholder="请输入收货人" v-model="form.user_name"
							placeholder-class="text-color-assist" />
					</view>
				</list-cell>
				<list-cell :hover="false">
					<view class="form-input">
						<view class="label">性别</view>
						<view class="radio-group">
							<view class="radio" :class="{'checked': !form.sex}" style="margin-right: 10rpx;"
								@tap="form.sex=0">先生</view>
							<view class="radio" :class="{'checked': form.sex}" @tap="form.sex=1">女士</view>
						</view>
					</view>
				</list-cell>
				<list-cell :hover="false">
					<view class="form-input">
						<view class="label">联系方式</view>
						<input class="input" placeholder="请输入收货人联系方式" v-model="form.mobile"
							placeholder-class="text-color-assist" />
					</view>
				</list-cell>
				<list-cell :hover="false">
					<view class="form-input">
						<view class="label" @click="selectAddress">选择地址</view>
						<input class="input" placeholder="请选择收货地址" v-model="form.accept_name" />
					</view>
				</list-cell>
				<list-cell :hover="false">
					<view class="form-input">
						<view class="label">详细地址</view>
						<input class="input" placeholder="请输入收货人详细地址" v-model="form.door_number"
							placeholder-class="text-color-assist" />
					</view>
				</list-cell>
			</view>
			<view class="btn-section">
				<button type="primary" size="default" @tap="save">保存</button>
			</view>
		</view>
	</view>
</template>

<script>
	import {mapState} from 'vuex'
	import listCell from '@/components/list-cell/list-cell'

	export default {
		components: {
			listCell,
			
		},
		computed:{
			...mapState(["openid"])
		},
		data() {
			return {
				form: {
					user_name: "",
					accept_name: '',
					sex: 0,
					mobile: '',
					door_number: '    ',
					address_id: null,
					openid:null,
				},
				data: null,
				opt:{
					url: "",
					method: "",
				},
				param:{
				
				},
				
			}
		},
		async onLoad({
			item
		}) {
			if (item != null) {
				this.data = item.replace(/""/g, "");
				this.data = JSON.parse(this.data)
				this.form.user_name = this.data.addressUserName
				this.form.accept_name = this.data.addressDetailed
				this.form.sex = this.data.addressUserSex
				this.form.mobile = this.data.addressUserPhone
				this.form.door_number = this.data.addressName
				this.form.address_id = this.data.addressId
				this.form.openid = this.openid
			}
			this.form.openid = this.openid

		},
		methods: {
			isAddress(){
			},
			save() {
				if(this.form.user_name===null||this.form.user_name===''){
					uni.showToast({
						title:"收货人姓名不能为空",
						icon:"none"
					})
					return; 
				}
				if(this.form.accept_name===null||this.form.accept_name===''){
					uni.showToast({
						title:"收货人地址不能为空",
						icon:"none"
					})
					return; 
				}
				if(this.form.mobile===null||this.form.mobile===''){
					uni.showToast({
						title:"收货人电话号码不能为空",
						icon:"none"
					})
					return; 
				}
				const isPhone = /^((1[3,5,8,7,9][0-9])|(14[5,7])|(17[0,6,7,8])|(19[1,7]))\d{8}$/;
				if(!isPhone.test(this.form.mobile)){
					uni.showToast({
						title:"电话号码格式不正确",
						icon:"none"
					})
					return; 
				}
				if (this.data !== null) {
					this.opt={
						url:"/address/editAddress",
						method:"post"
					},
					this.param= this.form
					this.$req.httpRequest(this.opt,this.param).then(res => {
						uni.navigateBack();
					})
				} else {
					this.opt={
						url:"/address/saveAddress",
						method:"post"
					}
					this.param = this.form
					console.log(this.param)
					this.$req.httpRequest(this.opt,this.param).then(()=>{
						uni.navigateBack()
					})
				}
			},
			selectAddress() {
				wx.chooseLocation({
					success: res => {
						this.form.accept_name = res.address
					},
					fail() {
						uni.showToast({
							icon: "none",
							title: "地址选择取消"
						})
					}

				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.form-box {
		width: 100%;
		height: 100%;
		padding: 30rpx;
		display: flex;
		flex-direction: column;
		color: $text-color-base;

		.form-input {
			display: flex;
			align-items: center;
			width: 100%;
		}

		.label {
			width: 200rpx;
			font-size: $font-size-lg;
			color: $text-color-base;
			font-weight: 500;
		}

		.input {
			flex: 1;
			display: flex;
			align-items: center;
		}

		.radio-group {
			display: flex;
			justify-content: flex-start;

			.radio {
				padding: 10rpx 30rpx;
				border-radius: 6rpx;
				border: 2rpx solid $text-color-assist;
				color: $text-color-assist;
				font-size: $font-size-base;

				&.checked {
					background-color: $color-primary;
					color: $text-color-white;
					border: 2rpx solid $color-primary;
				}
			}
		}

		.btn-section {
			flex: 1;
			display: flex;
			align-items: center;
			justify-content: center;

			button {
				font-size: $font-size-base;
				height: 90rpx;
				border-radius: 50rem !important;
				width: 85%;
				display: flex;
				align-items: center;
				justify-content: center;
			}
		}
	}
</style>
